package csc.fresher.commons;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("passwordValidator")
public class PasswordValidator implements Validator {

	@Override
	public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {
		// TODO Auto-generated method stub
		UIInput input = (UIInput) arg1.getAttributes().get("txtConfirm");
		String confirm = (String) input.getSubmittedValue();
		String pass = (String)arg2;
		if(pass == null || pass.isEmpty() || confirm == null || confirm.isEmpty()){
			return;
		}
		if(pass.length()<6) {
			input.setValid(false);
			throw new ValidatorException(new FacesMessage("Password: Validation Error: Length is less than allowable minimum of 6"));
		}
		else if (!pass.equals(confirm)) {
			input.setValid(false);
			throw new ValidatorException(new FacesMessage("Confirm Password is not match!"));
		}
	}

}
